package crond

import (
	"context"
	"encoding/json"
	"time"

	"system-admin/config/redis"

	"192.168.1.75/go-pkg/helper"
	"192.168.1.75/go-pkg/logx"
)

// SendSMS
func SendSMS() {
	for {
		// 获取短信请求
		redisCtx, redisCtxCancel := context.WithTimeout(context.Background(), time.Second*3)
		smsStr, err := redis.NewDB().RPop(redisCtx, "system-admin:sms-list").Result()
		redisCtxCancel()
		if err != nil {
			break
		}
		// 解析请求内容
		var smsReq helper.SMSReq
		json.Unmarshal([]byte(smsStr), &smsReq)

		// 短信发送实例
		sms := helper.NewSMS(
			"cn-hangzhou",
			"LTAI5tP8urB7dqsW35am7jPv",
			"hKB4Nb0yOf1YSWBoI8b6mUU6Pugacg",
			"大道智创科技",
		)
		// 发送短信
		_, err = sms.SendSms(
			smsReq.PhoneNumber,
			smsReq.Content,
			smsReq.TemplateCode,
		)
		if err != nil {
			logx.Error(context.Background(), "send sms failed", logx.Err(err))
		}
	}
}
